/**
 * Created by Administrator on 2017/8/2.
 */
$(document).ready(function () {

    var userTpl=$('#user-template').html();
    var userCmp=Handlebars.compile(userTpl);

if (C.token==undefined||C.token==null){
    alert('请登录');
    //location.href='login.html';
    againLogin();
    return ;
}
    $.ajax({
        url:C.interface.memberInfo,
        data:{
            token:C.token
        },
        dataType:'json',
        type:'get',
        success:function (response) {
            if (response.result=='success'){
                var data=response.data;
                $('#user-info-box').html(userCmp(data));

                //修改昵称
                $('.userNameInput2').hide();
                $('.userNameInput1').click(function () {
                    $('.userNameInput1').css('display', 'none');
                    $('.userNameInput2').show();

                    $('.confirm-button').unbind().bind('click',insertName);
                })
            }else if (response.result === 'login'){
                alert('登录过期请重新登录');
                againLogin();
                //alert(response.errorMsg);
            }
        }
    });
    function insertName(event) {
        event.stopPropagation();
        var name = $('.userName1').val();
        if (name.length >= 16 ||name.length == 0){
            alert('名字过长或过短，请重新输入');
        }else {
            var loading = dialog.loading();
            $.ajax({
                url: C.interface.name,
                type: 'post',
                dataType: 'json',
                data: {
                    token: C.token,
                    name: name
                },
                complete:function () {
                    loading.close()
                },
                success: function (data) {
                    console.log(data);
                    $('.userNameInput2').hide();
                    $('.userNameInput1').show();
                    $('.userName').html(name);
                }
            })
        }

    }


    














});